home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / test / test_re.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  10.5 KB  |  544 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. import sys
  5. sys.path = [
  6.     '.'] + sys.path
  7. TestFailed
  8. import re
  9. import sys
  10. import os
  11. import string
  12. import traceback
  13. if verbose:
  14.     print 'Running tests on re.search and re.match'
  15.  
  16.  
  17. try:
  18.     if not __debug__ and re.search('x*', 'axx').span(0) == (0, 0):
  19.         raise AssertionError
  20.     if not __debug__ and re.search('x*', 'axx').span() == (0, 0):
  21.         raise AssertionError
  22.     if not __debug__ and re.search('x+', 'axx').span(0) == (1, 3):
  23.         raise AssertionError
  24.     if not __debug__ and re.search('x+', 'axx').span() == (1, 3):
  25.         raise AssertionError
  26.     if not __debug__ and re.search('x', 'aaa') == None:
  27.         raise AssertionError
  28. except:
  29.     raise TestFailed, 're.search'
  30.  
  31.  
  32. try:
  33.     if not __debug__ and re.match('a*', 'xxx').span(0) == (0, 0):
  34.         raise AssertionError
  35.     if not __debug__ and re.match('a*', 'xxx').span() == (0, 0):
  36.         raise AssertionError
  37.     if not __debug__ and re.match('x*', 'xxxa').span(0) == (0, 3):
  38.         raise AssertionError
  39.     if not __debug__ and re.match('x*', 'xxxa').span() == (0, 3):
  40.         raise AssertionError
  41.     if not __debug__ and re.match('a+', 'xxx') == None:
  42.         raise AssertionError
  43. except:
  44.     raise TestFailed, 're.search'
  45.  
  46. if verbose:
  47.     print 'Running tests on re.sub'
  48.  
  49.  
  50. try:
  51.     if not __debug__ and re.sub('(?i)b+', 'x', 'bbbb BBBB') == 'x x':
  52.         raise AssertionError
  53.     
  54.     def bump_num(matchobj):
  55.         int_value = int(matchobj.group(0))
  56.         return str(int_value + 1)
  57.  
  58.     if not __debug__ and re.sub('\\d+', bump_num, '08.2 -2 23x99y') == '9.3 -3 24x100y':
  59.         raise AssertionError
  60.     if not __debug__ and re.sub('\\d+', bump_num, '08.2 -2 23x99y', 3) == '9.3 -3 23x99y':
  61.         raise AssertionError
  62.     if not __debug__ and re.sub('.', (lambda m: '\\n'), 'x') == '\\n':
  63.         raise AssertionError
  64.     if not __debug__ and re.sub('.', '\\n', 'x') == '\n':
  65.         raise AssertionError
  66.     s = '\\1\\1'
  67.     if not __debug__ and re.sub('(.)', s, 'x') == 'xx':
  68.         raise AssertionError
  69.     if not __debug__ and re.sub('(.)', re.escape(s), 'x') == s:
  70.         raise AssertionError
  71.     if not __debug__ and re.sub('(.)', (lambda m: s), 'x') == s:
  72.         raise AssertionError
  73.     if not __debug__ and re.sub('(?P<a>x)', '\\g<a>\\g<a>', 'xx') == 'xxxx':
  74.         raise AssertionError
  75.     if not __debug__ and re.sub('(?P<a>x)', '\\g<a>\\g<1>', 'xx') == 'xxxx':
  76.         raise AssertionError
  77.     if not __debug__ and re.sub('(?P<unk>x)', '\\g<unk>\\g<unk>', 'xx') == 'xxxx':
  78.         raise AssertionError
  79.     if not __debug__ and re.sub('(?P<unk>x)', '\\g<1>\\g<1>', 'xx') == 'xxxx':
  80.         raise AssertionError
  81.     if not __debug__ and re.sub('a', '\\t\\n\\v\\r\\f\\a\\b\\B\\Z\\a\\A\\w\\W\\s\\S\\d\\D', 'a') == '\t\n\x0b\r\x0c\x07\x08\\B\\Z\x07\\A\\w\\W\\s\\S\\d\\D':
  82.         raise AssertionError
  83.     if not __debug__ and re.sub('a', '\t\n\x0b\r\x0c\x07', 'a') == '\t\n\x0b\r\x0c\x07':
  84.         raise AssertionError
  85.     if not __debug__ and re.sub('a', '\t\n\x0b\r\x0c\x07', 'a') == chr(9) + chr(10) + chr(11) + chr(13) + chr(12) + chr(7):
  86.         raise AssertionError
  87.     if not __debug__ and re.sub('^\\s*', 'X', 'test') == 'Xtest':
  88.         raise AssertionError
  89. except AssertionError:
  90.     raise TestFailed, 're.sub'
  91.  
  92.  
  93. try:
  94.     if not __debug__ and re.sub('a', 'b', 'aaaaa') == 'bbbbb':
  95.         raise AssertionError
  96.     if not __debug__ and re.sub('a', 'b', 'aaaaa', 1) == 'baaaa':
  97.         raise AssertionError
  98. except AssertionError:
  99.     raise TestFailed, 'qualified re.sub'
  100.  
  101. if verbose:
  102.     print 'Running tests on symbolic references'
  103.  
  104.  
  105. try:
  106.     re.sub('(?P<a>x)', '\\g<a', 'xx')
  107. except re.error:
  108.     reason = None
  109.  
  110. raise TestFailed, 'symbolic reference'
  111.  
  112. try:
  113.     re.sub('(?P<a>x)', '\\g<', 'xx')
  114. except re.error:
  115.     reason = None
  116.  
  117. raise TestFailed, 'symbolic reference'
  118.  
  119. try:
  120.     re.sub('(?P<a>x)', '\\g', 'xx')
  121. except re.error:
  122.     reason = None
  123.  
  124. raise TestFailed, 'symbolic reference'
  125.  
  126. try:
  127.     re.sub('(?P<a>x)', '\\g<a a>', 'xx')
  128. except re.error:
  129.     reason = None
  130.  
  131. raise TestFailed, 'symbolic reference'
  132.  
  133. try:
  134.     re.sub('(?P<a>x)', '\\g<1a1>', 'xx')
  135. except re.error:
  136.     reason = None
  137.  
  138. raise TestFailed, 'symbolic reference'
  139.  
  140. try:
  141.     re.sub('(?P<a>x)', '\\g<ab>', 'xx')
  142. except IndexError:
  143.     reason = None
  144.  
  145. raise TestFailed, 'symbolic reference'
  146.  
  147. try:
  148.     re.sub('(?P<a>x)|(?P<b>y)', '\\g<b>', 'xx')
  149. except re.error:
  150.     reason = None
  151.  
  152. raise TestFailed, 'symbolic reference'
  153.  
  154. try:
  155.     re.sub('(?P<a>x)|(?P<b>y)', '\\2', 'xx')
  156. except re.error:
  157.     reason = None
  158.  
  159. raise TestFailed, 'symbolic reference'
  160. if verbose:
  161.     print 'Running tests on re.subn'
  162.  
  163.  
  164. try:
  165.     if not __debug__ and re.subn('(?i)b+', 'x', 'bbbb BBBB') == ('x x', 2):
  166.         raise AssertionError
  167.     if not __debug__ and re.subn('b+', 'x', 'bbbb BBBB') == ('x BBBB', 1):
  168.         raise AssertionError
  169.     if not __debug__ and re.subn('b+', 'x', 'xyz') == ('xyz', 0):
  170.         raise AssertionError
  171.     if not __debug__ and re.subn('b*', 'x', 'xyz') == ('xxxyxzx', 4):
  172.         raise AssertionError
  173.     if not __debug__ and re.subn('b*', 'x', 'xyz', 2) == ('xxxyz', 2):
  174.         raise AssertionError
  175. except AssertionError:
  176.     raise TestFailed, 're.subn'
  177.  
  178. if verbose:
  179.     print 'Running tests on re.split'
  180.  
  181.  
  182. try:
  183.     if not __debug__ and re.split(':', ':a:b::c') == [
  184.         '',
  185.         'a',
  186.         'b',
  187.         '',
  188.         'c']:
  189.         raise AssertionError
  190.     if not __debug__ and re.split(':*', ':a:b::c') == [
  191.         '',
  192.         'a',
  193.         'b',
  194.         'c']:
  195.         raise AssertionError
  196.     if not __debug__ and re.split('(:*)', ':a:b::c') == [
  197.         '',
  198.         ':',
  199.         'a',
  200.         ':',
  201.         'b',
  202.         '::',
  203.         'c']:
  204.         raise AssertionError
  205.     if not __debug__ and re.split('(?::*)', ':a:b::c') == [
  206.         '',
  207.         'a',
  208.         'b',
  209.         'c']:
  210.         raise AssertionError
  211.     if not __debug__ and re.split('(:)*', ':a:b::c') == [
  212.         '',
  213.         ':',
  214.         'a',
  215.         ':',
  216.         'b',
  217.         ':',
  218.         'c']:
  219.         raise AssertionError
  220.     if not __debug__ and re.split('([b:]+)', ':a:b::c') == [
  221.         '',
  222.         ':',
  223.         'a',
  224.         ':b::',
  225.         'c']:
  226.         raise AssertionError
  227.     if not __debug__ and re.split('(b)|(:+)', ':a:b::c') == [
  228.         '',
  229.         None,
  230.         ':',
  231.         'a',
  232.         None,
  233.         ':',
  234.         '',
  235.         'b',
  236.         None,
  237.         '',
  238.         None,
  239.         '::',
  240.         'c']:
  241.         raise AssertionError
  242.     if not __debug__ and re.split('(?:b)|(?::+)', ':a:b::c') == [
  243.         '',
  244.         'a',
  245.         '',
  246.         '',
  247.         'c']:
  248.         raise AssertionError
  249. except AssertionError:
  250.     raise TestFailed, 're.split'
  251.  
  252.  
  253. try:
  254.     if not __debug__ and re.split(':', ':a:b::c', 2) == [
  255.         '',
  256.         'a',
  257.         'b::c']:
  258.         raise AssertionError
  259.     if not __debug__ and re.split(':', 'a:b:c:d', 2) == [
  260.         'a',
  261.         'b',
  262.         'c:d']:
  263.         raise AssertionError
  264.     if not __debug__ and re.split('(:)', ':a:b::c', 2) == [
  265.         '',
  266.         ':',
  267.         'a',
  268.         ':',
  269.         'b::c']:
  270.         raise AssertionError
  271.     if not __debug__ and re.split('(:*)', ':a:b::c', 2) == [
  272.         '',
  273.         ':',
  274.         'a',
  275.         ':',
  276.         'b::c']:
  277.         raise AssertionError
  278. except AssertionError:
  279.     raise TestFailed, 'qualified re.split'
  280.  
  281. if verbose:
  282.     print 'Running tests on re.findall'
  283.  
  284.  
  285. try:
  286.     if not __debug__ and re.findall(':+', 'abc') == []:
  287.         raise AssertionError
  288.     if not __debug__ and re.findall(':+', 'a:b::c:::d') == [
  289.         ':',
  290.         '::',
  291.         ':::']:
  292.         raise AssertionError
  293.     if not __debug__ and re.findall('(:+)', 'a:b::c:::d') == [
  294.         ':',
  295.         '::',
  296.         ':::']:
  297.         raise AssertionError
  298.     if not __debug__ and re.findall('(:)(:*)', 'a:b::c:::d') == [
  299.         (':', ''),
  300.         (':', ':'),
  301.         (':', '::')]:
  302.         raise AssertionError
  303. except AssertionError:
  304.     raise TestFailed, 're.findall'
  305.  
  306. if verbose:
  307.     print 'Running tests on re.match'
  308.  
  309.  
  310. try:
  311.     m = re.match('a', 'a')
  312.     if not __debug__ and m.groups() == ():
  313.         raise AssertionError
  314.     m = re.match('(a)', 'a')
  315.     if not __debug__ and m.groups() == ('a',):
  316.         raise AssertionError
  317.     pat = re.compile('((a)|(b))(c)?')
  318.     if not __debug__ and pat.match('a').groups() == ('a', 'a', None, None):
  319.         raise AssertionError
  320.     if not __debug__ and pat.match('b').groups() == ('b', None, 'b', None):
  321.         raise AssertionError
  322.     if not __debug__ and pat.match('ac').groups() == ('a', 'a', None, 'c'):
  323.         raise AssertionError
  324.     if not __debug__ and pat.match('bc').groups() == ('b', None, 'b', 'c'):
  325.         raise AssertionError
  326.     if not __debug__ and pat.match('bc').groups('') == ('b', '', 'b', 'c'):
  327.         raise AssertionError
  328. except AssertionError:
  329.     raise TestFailed, 'match .groups() method'
  330.  
  331.  
  332. try:
  333.     m = re.match('(a)', 'a')
  334.     if not __debug__ and m.group(0) == 'a':
  335.         raise AssertionError
  336.     if not __debug__ and m.group(0) == 'a':
  337.         raise AssertionError
  338.     if not __debug__ and m.group(1) == 'a':
  339.         raise AssertionError
  340.     if not __debug__ and m.group(1, 1) == ('a', 'a'):
  341.         raise AssertionError
  342.     pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
  343.     if not __debug__ and pat.match('a').group(1, 2, 3) == ('a', None, None):
  344.         raise AssertionError
  345.     if not __debug__ and pat.match('b').group('a1', 'b2', 'c3') == (None, 'b', None):
  346.         raise AssertionError
  347.     if not __debug__ and pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c'):
  348.         raise AssertionError
  349. except AssertionError:
  350.     raise TestFailed, 'match .group() method'
  351.  
  352. if verbose:
  353.     print 'Running tests on re.escape'
  354.  
  355.  
  356. try:
  357.     p = ''
  358.     for i in range(0, 256):
  359.         p = p + chr(i)
  360.         if not __debug__ and re.match(re.escape(chr(i)), chr(i)) != None:
  361.             raise AssertionError
  362.         0
  363.         if not __debug__ and re.match(re.escape(chr(i)), chr(i)).span() == (0, 1):
  364.             raise AssertionError
  365.         range(0, 256)
  366.     
  367.     pat = re.compile(re.escape(p))
  368.     if not __debug__ and pat.match(p) != None:
  369.         raise AssertionError
  370.     if not __debug__ and pat.match(p).span() == (0, 256):
  371.         raise AssertionError
  372. except AssertionError:
  373.     raise TestFailed, 're.escape'
  374.  
  375. if verbose:
  376.     print 'Pickling a RegexObject instance'
  377.  
  378. import pickle
  379. pat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
  380. s = pickle.dumps(pat)
  381. pat = pickle.loads(s)
  382.  
  383. try:
  384.     if not __debug__ and re.I == re.IGNORECASE:
  385.         raise AssertionError
  386.     if not __debug__ and re.L == re.LOCALE:
  387.         raise AssertionError
  388.     if not __debug__ and re.M == re.MULTILINE:
  389.         raise AssertionError
  390.     if not __debug__ and re.S == re.DOTALL:
  391.         raise AssertionError
  392.     if not __debug__ and re.X == re.VERBOSE:
  393.         raise AssertionError
  394. except AssertionError:
  395.     raise TestFailed, 're module constants'
  396.  
  397. for flags in [
  398.     re.I,
  399.     re.M,
  400.     re.X,
  401.     re.S,
  402.     re.L]:
  403.     
  404.     try:
  405.         r = re.compile('^pattern$', flags)
  406.     except:
  407.         0
  408.         [
  409.             re.I,
  410.             re.M,
  411.             re.X,
  412.             re.S,
  413.             re.L]
  414.         print 'Exception raised on flag', flags
  415.  
  416.  
  417. *
  418. for t in tests:
  419.     sys.stdout.flush()
  420.     if len(t) == 5:
  421.         (pattern, s, outcome, repl, expected) = t
  422.     elif len(t) == 3:
  423.         (pattern, s, outcome) = t
  424.     else:
  425.         raise ValueError, ('Test tuples should have 3 or 5 fields', t)
  426.     
  427.     try:
  428.         obj = re.compile(pattern)
  429.     except re.error:
  430.         tests
  431.         tests
  432.         None if verbose else [
  433.             re.I,
  434.             re.M,
  435.             re.X,
  436.             re.S,
  437.             re.L]
  438.         if outcome == SYNTAX_ERROR:
  439.             pass
  440.         else:
  441.             print '=== Syntax error:', t
  442.     except KeyboardInterrupt:
  443.         raise KeyboardInterrupt
  444.     except:
  445.         tests
  446.         None if verbose else [
  447.             re.I,
  448.             re.M,
  449.             re.X,
  450.             re.S,
  451.             re.L]
  452.         print '*** Unexpected error ***', t
  453.         if verbose:
  454.             traceback.print_exc(file = sys.stdout)
  455.         
  456.  
  457.     
  458.     try:
  459.         result = obj.search(s)
  460.     except re.error:
  461.         tests
  462.         msg = tests
  463.         None if verbose else [
  464.             re.I,
  465.             re.M,
  466.             re.X,
  467.             re.S,
  468.             re.L]
  469.         print '=== Unexpected exception', t, repr(msg)
  470.     except:
  471.         tests
  472.  
  473.     if outcome == SYNTAX_ERROR:
  474.         pass
  475.     elif outcome == FAIL:
  476.         if result is None:
  477.             pass
  478.         else:
  479.             print '=== Succeeded incorrectly', t
  480.     elif outcome == SUCCEED:
  481.         if result is not None:
  482.             (start, end) = result.span(0)
  483.             vardict = {
  484.                 'found': result.group(0),
  485.                 'groups': result.group(),
  486.                 'flags': result.re.flags }
  487.             for i in range(1, 100):
  488.                 
  489.                 try:
  490.                     gi = result.group(i)
  491.                     if gi is None:
  492.                         gi = 'None'
  493.                 except IndexError:
  494.                     0
  495.                     0
  496.                     range(1, 100)
  497.                     gi = 'Error'
  498.                 except:
  499.                     0
  500.  
  501.                 vardict['g%d' % i] = gi
  502.             
  503.             for i in result.re.groupindex.keys():
  504.                 
  505.                 try:
  506.                     gi = result.group(i)
  507.                     if gi is None:
  508.                         gi = 'None'
  509.                 except IndexError:
  510.                     0
  511.                     0
  512.                     result.re.groupindex.keys()
  513.                     gi = 'Error'
  514.                 except:
  515.                     0
  516.  
  517.                 vardict[i] = gi
  518.             
  519.             repl = eval(repl, vardict)
  520.             if repl != expected:
  521.                 print '=== grouping error', t, repr(repl) + ' should be ' + repr(expected)
  522.             
  523.         else:
  524.             print '=== Failed incorrectly', t
  525.         if pattern[:2] != '\\B' and pattern[-2:] != '\\B':
  526.             obj = re.compile(pattern)
  527.             result = obj.search(s, pos = result.start(0), endpos = result.end(0) + 1)
  528.             if result == None:
  529.                 print '=== Failed on range-limited match', t
  530.             
  531.         
  532.         obj = re.compile(pattern, re.IGNORECASE)
  533.         result = obj.search(s)
  534.         if result == None:
  535.             print '=== Fails on case-insensitive match', t
  536.         
  537.         obj = re.compile(pattern, re.LOCALE)
  538.         result = obj.search(s)
  539.         if result == None:
  540.             print '=== Fails on locale-sensitive match', t
  541.         
  542.     
  543.  
  544.